﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using WfActivityLibrary.common;

namespace WfActivityLibrary.control
{
	public partial class UCYhxx: UserControl
    {
        private bool BmClick = false;
        private string selectYhCode = string.Empty;         //当前选择的用户编号
        public string flag = "yh";   //分类显示（默认：业务员；"yh":用户信息；"yg":员工信息）
        public string ztbz = "1";


        #region 初始化

        /// <summary>
        /// 用户双击事件
        /// 返回完整用户姓名，用户角色名称，用户角色，用户单位等用户详细信息
        /// </summary>
        public event MessagePassEventHandler YhxxEvent;

        /// <summary>
        /// 部门单击事件
        /// 返回部门信息，部门编号，部门名称
        /// </summary>
        public event MessagePassEventHandler BmxxEvent;

        public UCYhxx()
        {
            //生成客户端引用
            InitializeComponent();

            //初始化listview
            initListView();
            //获得用户树
            Update_Treeview();
            this.treeBm.AfterSelect += new TreeViewEventHandler(treeYh_AfterSelect);
            this.treeBm.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(treeBm_NodeMouseDoubleClick);
            this.lv_yhxx.DoubleClick += new EventHandler(lv_yhxx_DoubleClick);
            this.tab_js.Width = 0;
        }

        //初始化显示用户信息的listview
        private void initListView()
        {
            ListViewControlUtil.iniToDetail(lv_yhxx, false);
            Dictionary<string, object> data = new Dictionary<string, object>();
            data.Add(ListViewControlUtil.VIEW_HEAD,
                new string[,] { { "用户编号", "40" }, { "用户工号", "60" }, { "用户名", "60" }, { "职务名称", "60" } });
            ListViewControlUtil.fillListView(lv_yhxx, data);
            lv_yhxx.Columns[0].Width = 0;
        }

        #endregion


        #region 树的操作（创建，节点单击，节点双击事件）

        //填充根节点
        public void Update_Treeview()
        {
            //更新部门树
            string[][] bmxxs = new string[][] { new string[] { "1" } };//YhxxProx.getAllDwxx(ztbz);
            foreach (string[] bmxx in bmxxs)
            {
                TreeNode dw_nodes = new TreeNode();
                DwxxEntity dwxxentity = new DwxxEntity();
                dw_nodes.Text = bmxx[1];
                dwxxentity.dwCode = Convert.ToInt64(bmxx[0]);
                dwxxentity.dwName = bmxx[1];
                dw_nodes.Tag = dwxxentity;
                treeBm.Nodes.Add(dw_nodes);
            }
        }

        //部门树选中后
        void treeYh_AfterSelect(object sender, TreeViewEventArgs e)
        {
            lv_yhxx.Items.Clear();
            DataTable dt_yhxx = null;
            DwxxEntity dwxxentity = (DwxxEntity)treeBm.SelectedNode.Tag;
            if (flag == "")
                dt_yhxx = new DataTable();//YhxxProx.getYgxxByDwCodeYwyTable((int)dwxxentity.dwCode, "Y", ztbz);
            else if (flag == "yh")
                dt_yhxx = new DataTable();//YhxxProx.getYhxxByDwCodeTable((int)dwxxentity.dwCode, ztbz);
            else if (flag == "yg")
                dt_yhxx = new DataTable();//YhxxProx.getYgxxByDwCodeTable((int)dwxxentity.dwCode, ztbz);
            if (dt_yhxx != null && dt_yhxx.Rows.Count > 0)
            {
                for (int i = 0; i < dt_yhxx.Rows.Count; i++)
                {
                    DataRow dr = dt_yhxx.Rows[i];
                    lv_yhxx.Items.Add(dr[0].ToString().Trim());
                    lv_yhxx.Items[i].SubItems.Add(dr["yh_gh"].ToString().Trim());
                    lv_yhxx.Items[i].SubItems.Add(dr["yh_name"].ToString().Trim());
                    lv_yhxx.Items[i].SubItems.Add(dr["yh_zwmc"].ToString().Trim());
                }
            }
        }

        private void treeBm_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (this.BmClick)
            {
                DwxxEntity dwxxentity = (DwxxEntity)this.treeBm.SelectedNode.Tag;
                if (dwxxentity != null)
                {
                    MessagePassEventArgs cea = new MessagePassEventArgs(dwxxentity);
                    BmxxEvent(cea);
                    this.Visible = false;
                }
            }
        }

        #endregion

        #region listview操作(双击)

        private void lv_yhxx_DoubleClick(object sender, EventArgs e)
        {
            if (lv_yhxx.SelectedIndices.Count <= 0)
            {
                this.Visible = false;
                return;
            }
            else
                PassData();
        }

        #endregion

        #region 数据传送

        private void PassData()
        {
            int select_index = lv_yhxx.SelectedItems[0].Index;
            YhxxEntity yhxxEntity = new YhxxEntity();//YhxxProx.YhxxByYhcode1(Convert.ToInt64(lv_yhxx.Items[select_index].SubItems[0].Text), ztbz);

            MessagePassEventArgs cea = new MessagePassEventArgs(yhxxEntity);
            YhxxEvent(cea);
            this.Visible = false;
        }

        #endregion

        #region 工具栏

        private void toolStripOK_Click_1(object sender, EventArgs e)
        {
            MessagePassEventArgs cea = null;
            cea = new MessagePassEventArgs(null);
            YhxxEvent(cea);
            this.Visible = false;
        }

        private void toolStripCancle_Click_1(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        #endregion
	}
}
